package com.jason.custom;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.config.KafkaListenerEndpointRegistry;
import org.springframework.kafka.listener.MessageListenerContainer;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.annotation.Schedules;
import org.springframework.stereotype.Component;

/**
 * 前提：自动开启关闭  autoStartUp=false
 */
@Component
//@EnableScheduling
@Slf4j
public class CustomConsumeSet {

    @Autowired
    KafkaListenerEndpointRegistry registry;

    @Scheduled(cron = "0 35 20 * * ?")
    public void startConsume(){

        MessageListenerContainer listenerContainer = registry.getListenerContainer("myContainerId");
        if(!listenerContainer.isRunning()){
            log.info("~~~~~~消费开始~~~~~~~~");
            listenerContainer.start();  //消费开始
        }else{
            log.info("~~~~~~消费继续~~~~~~~~");
            listenerContainer.resume(); //继续消费
        }
    }

    @Scheduled(cron = "0 40 20 * * ?")
    public void stopConsume(){
        MessageListenerContainer listenerContainer = registry.getListenerContainer("myContainerId");
        listenerContainer.stop();
        log.info("~~~~~~消费停止~~~~~~~~");
    }
}
